package com.kongge.viewdisplayframeworklib.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SPConfig {

	// ~常量区块
	// ================SP文件名==start======================================
	public static final String FILE_APP_INFO = "app_info";
	public static final String FILE_LOGIN = "sp_login";
	
	// ================SP文件名==end======================================
	
	// ================SP属性名==start======================================
	
	// -----------------FILE_APP_INFO----------------------------------------------------
	public static final String APP_INFO_SERVER_TYPE = "server_type";
	// -----------------FILE_LOGIN----------------------------------------------------
	public static final String LOGIN_USER_ACCOUNT = "sp_user_account";
	
	
	// ================SP属性名==end======================================

	// ~成员变量区块
	// ========================================================

	// ~构造函数区块
	// ========================================================

	// ~方法区块
	// ========================================================

	// ~静态方法区块
	// ========================================================
	/**
	 * 保存数据
	 * 
	 * @param context
	 * @param spName sharedpreferences 文件名
	 * @param key 
	 * @param value
	 */
	public static void saveStringSPValue(Context context, String spName, String key, Object value) {
		if (context == null) {
			return;
		}
		Editor editor = context.getSharedPreferences(spName, Context.MODE_WORLD_WRITEABLE).edit();
		if (value instanceof String) {
			editor.putString(key, (String)value);
		} else if (value instanceof Integer) {
			editor.putInt(key, (Integer)value);
		} else if (value instanceof Long) {
			editor.putLong(key, (Long)value);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean)value);
		}
		editor.commit();
	}
	
	/**
	 * 获取String类型数据
	 * @param context
	 * @param spName
	 * @param key
	 * @return
	 */
	public static String getStringSPValue(Context context, String spName, String key, String defaultValue) {
		String value = defaultValue;
		if (context == null) {
			return null;
		}
		SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_WORLD_WRITEABLE);
		if (sp != null && key != null) {
			value = sp.getString(key, null);
		}
		return value;
	}
	
	/**
	 * 获取int类型数据
	 * @param context
	 * @param spName
	 * @param key
	 * @return
	 */
	public static int getIntSPValue(Context context, String spName, String key, int defaultValue) {
		int value = defaultValue;
		if (context == null) {
			return value;
		}
		SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_WORLD_WRITEABLE);
		if (sp != null && key != null) {
			value = sp.getInt(key, 0);
		}
		return value;
	}
	
	/**
	 * 获取long类型数据
	 * @param context
	 * @param spName
	 * @param key
	 * @return
	 */
	public static long getLongSPValue(Context context, String spName, String key, long defaultValue) {
		long value = defaultValue;
		if (context == null) {
			return value;
		}
		SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_WORLD_WRITEABLE);
		if (sp != null && key != null) {
			value = sp.getLong(key, 0l);
		}
		return value;
	}
	
	/**
	 * 获取boolean类型数据
	 * @param context
	 * @param spName
	 * @param key
	 * @return
	 */
	public static boolean getBooleanSPValue(Context context, String spName, String key, boolean defaultValue) {
		boolean value = defaultValue;
		if (context == null) {
			return value;
		}
		SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_WORLD_WRITEABLE);
		if (sp != null && key != null) {
			value = sp.getBoolean(key, false);
		}
		return value;
	}
	

	// ~内部接口（类）区块
	// ========================================================

}

